/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trunk.view;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import trunk.model.PaintModel;


/**
 * Class DrawTabbed 
 * 
 * Components representant la gestion des onglets
 * 
 * @author LEFEVRE Michael
 * @author THOUE Jonathan
 */
public class DrawTabbed extends JTabbedPane{
    
    PaintModel model;
    
    /**
     * constructeur de la classe
     * 
     * @param model - lien avec le model général
     */
    public DrawTabbed(PaintModel model) {
        this.model = model;
        TabTitleEditListener l = new TabTitleEditListener(this);
        this.addChangeListener(l);
        this.addMouseListener(l);
        
        this.addTab(model.getDessin().getName(), new EnvironnementDessin(model));
    }
    
    
    /**
     * Class TabTitleEditListener
     * 
     * Listenner permettant de renommer un onglet
     * 
     */
    class TabTitleEditListener extends MouseAdapter implements ChangeListener{
        private final JTextField editor = new JTextField();
        private final JTabbedPane tabbedPane;
        /**
         * constructeur de la classe
         * 
         * @param tabbedPane - Gestionnaire d'onglet à écouter
         */
        public TabTitleEditListener(final JTabbedPane tabbedPane) {
            this.tabbedPane = tabbedPane;
            editor.setBorder(BorderFactory.createEmptyBorder());
            editor.addFocusListener(new FocusAdapter() {
                @Override public void focusLost(FocusEvent e) {
                    renameTabTitle();
                }
            });
            editor.addKeyListener(new KeyAdapter() {
                @Override public void keyPressed(KeyEvent e) {
                    if(e.getKeyCode()==KeyEvent.VK_ENTER) {
                        renameTabTitle();
                    }else if(e.getKeyCode()==KeyEvent.VK_ESCAPE) {
                        cancelEditing();
                    }else{
                        editor.setPreferredSize((editor.getText().length()>len)?null:dim);
                        tabbedPane.revalidate();
                    }
                }
            });
            tabbedPane.getInputMap(JComponent.WHEN_FOCUSED).put(
                KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "start-editing");
            tabbedPane.getActionMap().put("start-editing", new AbstractAction() {
                @Override public void actionPerformed(ActionEvent e) {
                    startEditing();
                }
            });
        }
        /**
         * Methode appler quand l'état change
         * 
         * @param e - event
         */
        @Override public void stateChanged(ChangeEvent e) {
            renameTabTitle();
        }
        /**
         * Methode appler quand la souris est cliqué
         * 
         * @param me - event de la souris
         */
        @Override public void mouseClicked(MouseEvent me) {
            Rectangle rect = tabbedPane.getUI().getTabBounds(tabbedPane, tabbedPane.getSelectedIndex());
            if(rect!=null && rect.contains(me.getPoint()) && me.getClickCount()==2) {
                startEditing();
            }else{
                renameTabTitle();
            }
        }
        private int editing_idx = -1;
        private int len = -1;
        private Dimension dim;
        private Component tabComponent = null;
        /**
         * Methode appeler quand on edite le titre de l'onglet
         */
        private void startEditing() {
            editing_idx = tabbedPane.getSelectedIndex();
            tabComponent = tabbedPane.getTabComponentAt(editing_idx);
            tabbedPane.setTabComponentAt(editing_idx, editor);
            editor.setVisible(true);
            editor.setText(tabbedPane.getTitleAt(editing_idx));
            editor.selectAll();
            editor.requestFocusInWindow();
            len = editor.getText().length();
            dim = editor.getPreferredSize();
            editor.setMinimumSize(dim);
        }
        /**
         * Methode appeler quand on finit l'edition
         */
        private void cancelEditing() {
            if(editing_idx>=0) {                
                tabbedPane.setTabComponentAt(editing_idx, tabComponent);
                editor.setVisible(false);
                editing_idx = -1;
                len = -1;
                tabComponent = null;
                editor.setPreferredSize(null);
                tabbedPane.requestFocusInWindow();                
            }
        }
        /**
         * Methode appeler pour renommer le titre de l'onglet
         */
        private void renameTabTitle() {
            String title = editor.getText().trim();
            if(editing_idx>=0 && !title.isEmpty()) {
                tabbedPane.setTitleAt(editing_idx, title);
                model.setTitle(title);
            }
            cancelEditing();
        }
    }
}
